/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import ejb.Warehouse;
import ejb.WarehouseFacadeLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author longdm0708l
 */
public class WarehouseBean {

    public void createWarehouse(Warehouse warehouse)
    {
        lookupWarehouseFacade().create(warehouse);
    }

    public void editWarehouse(Warehouse warehouse)
    {
        lookupWarehouseFacade().edit(warehouse);
    }

    public void removeWarehouse(Warehouse warehouse)
    {
        lookupWarehouseFacade().remove(warehouse);
    }

    private int warehouseId;

    public int getWarehouseId() {
        return warehouseId;
    }

    public void setWarehouseId(int warehouseId) {
        this.warehouseId = warehouseId;
    }

    public List<Warehouse> getAllWarehouse()
    {
        return lookupWarehouseFacade().findAll();
    }

    public Warehouse getWarehouseById()
    {
        return lookupWarehouseFacade().find(Integer.valueOf(getWarehouseId()));
    }

    private WarehouseFacadeLocal lookupWarehouseFacade() {
        try {
            Context c = new InitialContext();
            return (WarehouseFacadeLocal) c.lookup("java:comp/env/WarehouseFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
